Skip to content

Thread-safe promise and nodiscard - #20

Merged
iv461 merged 10 commits into
mainfrom
feature/promise_cleanups
Feb 20, 2026
Merged

Thread-safe promise and nodiscard#20
iv461 merged 10 commits into
mainfrom
feature/promise_cleanups

Conversation

@iv461

@iv461 iv461 commented Feb 20, 2026

Copy link
Copy Markdown
Owner

Changes to the Promise:

  • Cancellation is removed from the destructor, as it is unnecessary. The asynchronous operation is launched only in await_suspend, which always suspends the coroutine. Therefore the promise is stored in the coroutine state and cannot be destroyed until the coroutine resumes. We also disallow explicit cancellation because I don't know of any use case for it. The only remaining case is when the promise is destructed before it is awaited and therefore the launc operation started. This only happens if the user forgets to call co_await. To address this, I'm adding a nodiscard tag that introduces a compile-time warning.

  • Arbitrating the race between resolution and rejection using an atomic exchange. (first step of making it thread-safe)

  • Adds nodiscard annotations and a method for explicitly detaching the outermost coroutine that is called from a regular ROS callback where it is not awaited. Detaching simply means that the coroutine state is kept alive after the wrapper promise goes out of scope.

Other cleanups:

  • Small refactoring to use std::weak_ptr instead of raw weak references to clarify the intent.
  • Using C++20 concepts instead of C++14 SFINAE-style type traits.
  • Improves the Result type by adding move operations and nodiscard, as well as making operator bool explicit.
  • PromiseState is now an std::optional<Result>, is simpler albeit taking more space.

@iv461
iv461 merged commit 389b205 into main Feb 20, 2026
3 checks passed
@iv461
iv461 deleted the feature/promise_cleanups branch February 20, 2026 01:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant